import 'package:cached_network_image/cached_network_image.dart';
import 'package:e_book/model/book.dart';
import 'package:e_book/utils/header_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class MyBookTileItem extends StatelessWidget {
  final Book book;
  final double? height;
  final double? width;
  final bool? showPrice;
  final bool? showRate;
  const MyBookTileItem(
      {super.key,
      required this.book,
      this.height,
      this.width,
      this.showPrice,
      this.showRate});

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.only(right: 15.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Stack(
            children: [
              // 封面
              Container(
                width: width,
                height: height,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(12.r),
                  image: DecorationImage(
                      image: CachedNetworkImageProvider(
                        book.cover ?? '',
                        headers: HeaderUtil.randomHeader(),
                      ),
                      fit: BoxFit.cover),
                ),
              ),

              //价格 灵活显示
              _getPriceUI(context),
            ],
          ),
          // 标题
          Container(
            padding: EdgeInsets.only(top: 10.h),
            width: width,
            child: Text(
              book.title ?? '',
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(
                fontSize: 14.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
          ),
          // 副标题 作者
          _getSubTitleUI(context),

          // 评分
          _getRateUI(context)
        ],
      ),
    );
  }

  Widget _getSubTitleUI(BuildContext context) {
    if (book.subTitle == null && book.authorName == null) {
      return const SizedBox();
    }
    return Container(
      padding: EdgeInsets.only(top: 5.h),
      width: width,
      child: Text(
        book.subTitle ?? book.authorName ?? '',
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(
            fontSize: 12.sp,
            fontWeight: FontWeight.w500,
            color: Theme.of(context).colorScheme.inversePrimary),
      ),
    );
  }

  Widget _getRateUI(BuildContext context) {
    if (showRate != true) {
      return const SizedBox();
    }
    return Container(
      margin: REdgeInsets.only(top: 6.h),
      width: width,
      child: Row(
        children: [
          // 构建评分条，用于显示和更新书籍评分
          RatingBar.builder(
            itemCount: 5, // 设置评分的最大值为5
            itemSize: 15.r, // 设置评分图标大小为15.r（根据设备尺寸自适应的大小）
            itemPadding:
                EdgeInsets.only(right: 2.w), // 设置评分图标之间的间距为2.w（根据设备尺寸自适应的间距）
            ignoreGestures: true, // 禁用手势响应，评分条只显示不响应用户操作
            initialRating:
                (book.rate ?? 0.0) / 2, // 初始评分为书籍评分的一半，若书籍评分为null，则初始评分为0
            minRating: 1, // 最小评分值为1，用户不能选择低于1的评分
            direction: Axis.horizontal, // 评分条水平排列
            allowHalfRating: true, // 允许半星评分，增加评分的精细度
            itemBuilder: (context, _) {
              // 定义评分条的图标和颜色
              return Icon(
                Icons.star, // 使用星星图标作为评分图标
                color:
                    Theme.of(context).colorScheme.tertiary, // 图标颜色根据主题的三级颜色方案确定
              );
            },
            onRatingUpdate: (rating) {}, // 当评分更新时的回调函数，此处未实现具体逻辑
          )
        ],
      ),
    );
  }

  Widget _getPriceUI(BuildContext context) {
    if (showPrice == false) {
      return const SizedBox();
    }
    return Positioned(
      bottom: 0,
      child: Container(
        width: 65.w,
        height: 25.h,
        decoration: BoxDecoration(
            color: Theme.of(context).colorScheme.primary,
            borderRadius: BorderRadius.only(
              topRight: Radius.circular(12.r),
              bottomLeft: Radius.circular(12.r),
            )),
        child: Center(
          child: Text(
            '￥12.0',
            style: TextStyle(
                color: Theme.of(context).colorScheme.onPrimary,
                fontSize: 12.sp,
                fontWeight: FontWeight.w600),
          ),
        ),
      ),
    );
  }
}
